package leetcode.code019.RemoveNthNodeFromEndofList;


import java.util.Stack;

import leetcode.helper.tree.ListNode;

/**
 * 一次 + stack
 *
 * @author Max
 * @date 2021年8月16日
 * @description
 */
public class Solution3 extends RemoveNthNodeFromEndofList {

	@Override
	public ListNode removeNthFromEnd(ListNode head, int n) {
		Stack<ListNode> s = new Stack<>();
		ListNode p = head;
		while (p != null) {
			s.add(p);
			p = p.next;
		}
		while (!s.empty() && n > 0) {
			s.pop();
			n--;
		}
		if (s.empty()) {
			return head.next;
		} else {
			p = s.pop();
			p.next = p.next.next;
		}
		return head;
	}

	public static void main(String[] args) {
		RemoveNthNodeFromEndofList so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
//		so.debug4();
	}

}
